import React, { Component } from 'react';

import './index.less';

import Tree from '@/components/Tree';
import Table from '@/components/Table';

import { Modal } from 'antd';
import Context from '@/components/context'

export default class ModalCm extends Component {

  constructor(props) {
    super(props)
    this.state = {
      visible: true,
      confirmLoading: false,
      tabCell: {},
      id: '',
      name: '',
      searchVal: '',
      current: 1,
    }
    this.treeData = {}
  }

  componentWillReceiveProps(nextP) {
    if (nextP.isClickTree) {
      if (nextP.modalLinkType === 'BM') {
        if (nextP.BM) {
          this.searchTableFn(nextP.BM)
        }
      } else {
        if (nextP.goodsName) {
          console.log('name', nextP)
          this.searchTableFn(nextP.goodsName)
        }
      }

    }
  }

  // 关闭弹出
  handleCancel = () => {
    this.props.closeModal();
  }


  //页码改变
  pageChangeFn(res) {
    this.treeData.current = res;
    console.log(res, this.treeData.current, 'this.current');
    this.treeSelect()
  }

  // 请求数据
  treeSelect(param) {
    if (param) {
      this.treeData.id = param.id;
      this.treeData.name = param.name;
      this.treeData.current = param.current;
    }
    this.$ajax.get('/bmjc/getTreeChild.do', { params: { name: this.treeData.name, id: this.treeData.id, current: this.treeData.current, pCount: 15 } })
      .then(res => {
        if (res.code !== 200 || (res.code === 200 && res.data.list.length === 0)) {
          this.setState({
            tabCell: '',
            searchVal: true
          })
        } else {
          this.setState({
            tabCell: res.data,
            searchVal: true
          })
        }
      })
  }

  // table 页码搜索 处理函数
  searchTableFn(value) {
    this.$ajax.get('/bmjc/searchStandard.do', { params: { name: value } })
      .then(res => {
        if (res.code !== 200 || (res.code === 200 && res.data.length === 0)) {
          // alert('没有数据：getStandardTree')
          this.setState({
            tabCell: '',
            searchVal: false
          })
        } else {
          // console.log(res)
          const SearchData = {
            aCount: res.data.length,
            list: res.data
          }
          this.searchTableData = SearchData
          this.setState({
            tabCell: SearchData,
            searchVal: false
          })
        }
      })
  }

  render() {
    const { showModal, BM, goodsName, modalLinkType } = this.props
    return (
      <div>
        <Modal title="税收编码选择"
          width='1000px'
          maskClosable={false}
          visible={showModal}
          onCancel={this.handleCancel}
          footer={null}
          destroyOnClose={true}
          zIndex={1001}
        >
          <div className='modalClass'>
            <div className='treeClass'>
              <Tree current={this.treeData.current} treeSelect={(res) => this.treeSelect(res)} />
            </div>
            <div className='tableClass'>
              <Context.Consumer >
                {(v) =>
                  <Table searchVal={this.state.searchVal}
                    searchTableData={modalLinkType === 'BM' ? BM : goodsName}
                    tabCell={this.state.tabCell}
                    closeMod={() => this.handleCancel()}
                    pageChange={(res) => this.pageChangeFn(res)}
                    searchTable={(res) => this.searchTableFn(res)}
                    doubleClick={v}
                  />}
              </Context.Consumer>
            </div>
          </div>
        </Modal>

      </div>
    )
  }
}
